package com.meiya.hugegraph.api;

import org.apache.hugegraph.driver.HugeClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.meiya.hugegraph.config.HugeGraphConfig;

/**
 * HugeGraph客户端工厂类
 * 统一管理HugeClient实例的创建和关闭
 */
@Component
public class HugeGraphClientFactory {
    
    private static final Logger logger = LoggerFactory.getLogger(HugeGraphClientFactory.class);
    private static HugeGraphConfig config;
    
    /**
     * 注入配置
     * 
     * @param hugeGraphConfig HugeGraph配置
     */
    @Autowired
    public void setConfig(HugeGraphConfig hugeGraphConfig) {
        config = hugeGraphConfig;
    }
    
    /**
     * 创建默认配置的HugeClient实例
     * 
     * @return HugeClient实例
     */
    public static HugeClient createClient() {
        String serverUrl = config != null ? config.getServerUrl() : "http://localhost:8080";
        String graphName = config != null ? config.getGraphName() : "hugegraph";
        int timeout = config != null ? config.getTimeout() : 60;
        
        return createClient(serverUrl, graphName, timeout);
    }
    
    /**
     * 创建指定图的HugeClient实例
     * 
     * @param graphName 图名称
     * @return HugeClient实例
     */
    public static HugeClient createClient(String graphName) {
        String serverUrl = config != null ? config.getServerUrl() : "http://localhost:8080";
        int timeout = config != null ? config.getTimeout() : 60;
        
        return createClient(serverUrl, graphName, timeout);
    }
    
    /**
     * 创建指定服务器和图的HugeClient实例
     * 
     * @param serverUrl 服务器URL
     * @param graphName 图名称
     * @return HugeClient实例
     */
    public static HugeClient createClient(String serverUrl, String graphName) {
        int timeout = config != null ? config.getTimeout() : 60;
        return createClient(serverUrl, graphName, timeout);
    }
    
    /**
     * 创建完整自定义配置的HugeClient实例
     * 
     * @param serverUrl 服务器URL，例如 http://localhost:8080
     * @param graphName 图名称，例如 hugegraph
     * @param timeout   超时时间（秒）
     * @return HugeClient实例
     */
    public static HugeClient createClient(String serverUrl, String graphName, int timeout) {
        logger.info("创建HugeClient连接，服务器: {}，图: {}", serverUrl, graphName);
        
        try {
            HugeClient client = HugeClient.builder(serverUrl, graphName)
                    .configTimeout(timeout)
                    .build();
            
            logger.info("HugeClient创建成功");
            return client;
        } catch (Exception e) {
            logger.error("创建HugeClient失败", e);
            throw new RuntimeException("创建HugeClient失败: " + e.getMessage(), e);
        }
    }
    
    /**
     * 安全关闭HugeClient
     * 
     * @param client 要关闭的客户端实例
     */
    public static void closeClient(HugeClient client) {
        if (client != null) {
            try {
                client.close();
                logger.info("HugeClient已关闭");
            } catch (Exception e) {
                logger.error("关闭HugeClient时发生错误", e);
            }
        }
    }
} 